#ifndef TABLEMODEL_H
#define TABLEMODEL_H

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QAbstractTableModel>

class TableModel : public QAbstractTableModel
{
    Q_OBJECT
private:
    QVector<QStringList> m_data;
    QStringList m_header;
public:

    int rowCount(const QModelIndex & = QModelIndex()) const override
    {
        return m_data.size();
    }

    int columnCount(const QModelIndex & = QModelIndex()) const override
    {
        return m_header.size();
    }

    QVariant data(const QModelIndex &index, int role) const override
    {
        switch (role) {
            case Qt::DisplayRole:
                return m_data[index.row()][index.column()];
                return QString("%1, %2").arg(index.column()).arg(index.row());
            default:
                break;
        }

        return QVariant();
    }



    Q_INVOKABLE QVariant headerData(int section, Qt::Orientation orientation ,int role) const override
    {
        //返回在Cities字符串列表中给定偏移量的城市名称
        if (role == Qt::DisplayRole)
            return m_header[section];
        return QAbstractTableModel::headerData(section,orientation,role);
    }

    QHash<int, QByteArray> roleNames() const override
    {
        return { {Qt::DisplayRole, "display"} };
    }
public slots:
//    void setColCount(int n) {

//    }

    void addHeader(QString header) {
        m_header.push_back(header);
    }

    void addRow(QStringList content) {
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        m_data.push_back(content);
        endInsertRows();
    }


};

#endif // TABLEMODEL_H
